/*
 * File Name:               Inverter_BTree.cs
 * 
 * Description:             普通类
 * Author:                  lisiyu <576603306@qq.com>

 * Create Date:             2017/06/13
 */

using BehaviorDesigner.Runtime.Tasks;
using System;

namespace XBehaviorTree
{
    public class Inverter_BTree : ITask_BTree
#if XBTREE_DEBUG
        , ITask_Debug
#endif
    {
        public ITask_BTree mChild;

#if XBTREE_DEBUG
        public string FriendlyName { get; set; }
        public TaskStatus OnDebugUpdate()
        {
            if (mChild == null)
            {
                DebugStatus = TaskStatus.Failure;
                return DebugStatus;
            }

            OnResetDebugStatus();

            if ((mChild as ITask_Debug).OnDebugUpdate() == TaskStatus.Success)
                DebugStatus = TaskStatus.Failure;
            else
                DebugStatus = TaskStatus.Success;

            return DebugStatus;
        }

        public void OnResetDebugStatus()
        {
            DebugStatus = TaskStatus.Inactive;
            (mChild as ITask_Debug).OnResetDebugStatus();
        }

        public TaskStatus DebugStatus { get; set; }
        public bool ViewChild { get; set; }
        public Inverter_BTree(string friendlyName, ITask_BTree child)
        {
            FriendlyName = friendlyName;
            mChild = child;
        }
#endif

        public Inverter_BTree(ITask_BTree child)
        {
            mChild = child;
        }

        public TaskStatus OnUpdate()
        {
            if (mChild == null)
                return TaskStatus.Failure;

            if ((int)mChild.OnUpdate() == (int)TaskStatus.Success)
                return TaskStatus.Failure;
            else
                return TaskStatus.Success;
        }

        public static ITask_BTree parse(Inverter inverter)
        {
            var child = XBehaviorTree.parse(inverter.Children[0]);
#if XBTREE_DEBUG
            return new Inverter_BTree(inverter.FriendlyName, child);
#else
            return new Inverter_BTree(child);
#endif
        }
    }
}
